/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package defcore

import (
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/common/logging"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/core/cryptosuite/csp/multisuite"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/core/logging/api"
	core "git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/core"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/ichain"

	signingMgr "git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain/signingmgr"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain_sdk/provider/ichainpvdr"

	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/core/logging/modlog"
)

var logger = logging.NewLogger("ichainsdk")

// ProviderFactory sdk提供者工厂
type ProviderFactory struct {
}

func NewProviderFactory() *ProviderFactory {
	f := ProviderFactory{}
	return &f
}

// CreateCryptoSuite 创建加密
// @param config
// @return core.CryptoSuite
// @return error
func (f *ProviderFactory) CreateCryptoSuite(config core.CryptoSuiteConfig) (core.CryptoSuite, error) {
	if config.SecurityProvider() != "sw" && config.SecurityProvider() != "gm" {
		logger.Warnf("default provider factory doesn't support '%s' crypto provider", config.SecurityProvider())
	}

	var err error
	var cryptoSuiteProvider core.CryptoSuite
	cryptoSuiteProvider, err = multisuite.GetSuiteByConfig(config)
	return cryptoSuiteProvider, err
}

// CreateSigningManager 创建签名
// @param cryptoProvider
// @return core.SigningManager
// @return error
func (f *ProviderFactory) CreateSigningManager(cryptoProvider core.CryptoSuite) (core.SigningManager, error) {
	return signingMgr.New(cryptoProvider)
}

// CreateIChainProvider 创建ichain组件-inode节点、共识节点、通信模块
func (f *ProviderFactory) CreateIChainProvider(config ichain.EndpointConfig) (ichain.IChainProvider, error) {
	return ichainpvdr.New(config), nil
}

func NewLoggerProvider() api.LoggerProvider {
	return modlog.LoggerProvider()
}
